using System.Collections.Generic;
using System.Drawing;
using System.IO;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class GirlfriendMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.gf_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	private static List<dynamic> DefaultSkinList = new List<object>(new string[0]);

	public static string DefaultSkinGF = "Default";

	public GirlfriendMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnItemSelect += OnItemSelect;
			menu.OnMenuClose += OnMenuClose;
			isMenuInitialized = true;
		}
	}

	public static void InitGirlfriendMenu()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_DressingRoom));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_DriveToWaypoint));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_Clean));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_Settings));
		menu.BindMenuToItem(SettingMenu.menu, menu.MenuItems[3]);
		menu.AddItem(new UIMenuItem(TextSetting.gf_MenuItem_PreLoaded));
		menu.BindMenuToItem(PreLoadedMenu.menu, menu.MenuItems[4]);
		menu.AddItem(new UIMenuItem(TextSetting.gf_MenuItem_ReCreate));
		
		// 只在必要时调用RefreshIndex
		if (Main.P2.Character == null || Main.P2.Character.IsDead)
		{
			for (int i = 0; i < menu.MenuItems.Count; i++)
			{
				menu.MenuItems[i].Enabled = false;
			}
			menu.MenuItems[4].Enabled = true;
			menu.MenuItems[5].Enabled = true;
			return;
		}
		for (int j = 0; j < menu.MenuItems.Count; j++)
		{
			menu.MenuItems[j].Enabled = true;
		}
		if (Game.Player.Character.IsInVehicle())
		{
			menu.MenuItems[0].Enabled = false;
			menu.MenuItems[1].Enabled = true;
		}
		else
		{
			menu.MenuItems[0].Enabled = true;
			menu.MenuItems[1].Enabled = false;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		switch (index)
		{
		case 0:
			Game.FadeScreenOut(1);
			DressingScript.InitDressing(Main.Member.P2);
			DressingRoomMenu.InitDressingRoom();
			// 选择子菜单时隐藏当前菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			DressingRoomMenu.menu.Visible = true;
			Game.FadeScreenIn(500);
			break;
		case 1:
			Main.P2.DetectDriverAndWaypoint();
			// 选择功能时隐藏菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 2:
			Main.P2.Character.ClearBloodDamage();
			Function.Call(Hash._0x9C720776DAA43E7E, Main.P2.Character);
			Main.P2.Character.ResetVisibleDamage();
			Main.P2.Character.RemoveHelmet(instantly: true);
			if (Main.P2.Character.CurrentVehicle != null && Main.P2.Character.IsInVehicle(Main.P2.Character.CurrentVehicle))
			{
				Main.P2.Character.CurrentVehicle.Wash();
				Main.P2.Character.CurrentVehicle.Repair();
			}
			// 执行功能后隐藏菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 3:
			SettingMenu.InitCharacterSettingMenu(Main.Member.P2);
			// 选择子菜单时隐藏当前菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 4:
			PreLoadedMenu.InitPreLoadedMenu();
			// 选择子菜单时隐藏当前菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 5:
		{
			if (Main.P2.Character != null)
			{
				Main.P2.Character.MarkAsNoLongerNeeded();
				Main.P2.Character.Delete();
			}
			Ped[] nearbyPeds = World.GetNearbyPeds(Game.Player.Character.Position, 100f);
			foreach (Ped ped in nearbyPeds)
			{
				if ((ped.IsInGroup || ped.IsDead) && ped.Handle != Game.Player.Character.Handle)
				{
					ped.MarkAsNoLongerNeeded();
					ped.Delete();
				}
			}
			Main.P2.Character = null;
			Main.InitFemale();
			new DirectoryInfo($"{Skin.SKIN_PATH}");
			if (File.Exists($"{Skin.SKIN_PATH}\\{Gender.Female}\\{DefaultSkinGF}.skin"))
			{
				Skin.SkinFileLoad(ref Main.P2, $"{Skin.SKIN_PATH}\\{Gender.Female}\\{DefaultSkinGF}.skin");
			}
			if (File.Exists($"{Skin.SKIN_PATH}\\{Gender.Male}\\{DefaultSkinGF}.skin"))
			{
				Skin.SkinFileLoad(ref Main.P2, $"{Skin.SKIN_PATH}\\{Gender.Male}\\{DefaultSkinGF}.skin");
			}
			Main.P2.skin.SetWeapons(Main.P1.skin.GetWeaponsData());
			// 执行功能后隐藏菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		}
		}
	}

	private void OnMenuClose(UIMenu sender)
	{
	}
}
